<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;
use utils\Data;

class DataType extends AdminBase
{
    public function index()
    {
        $list = Db::name('data_type')->order('sort asc, id asc')->select()->toArray(); 
        $list = Data::tree($list, 'title','id');
        return view('',['list'=>$list])->filter(function($content){
            return str_replace("&amp;emsp;",'&emsp;',$content);
        });
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if($param['pid']){
                $data = Db::name('data_type')->field('level')->find($param['pid']);
                $param['level'] = ($data['level']+1);
            }else{
                $param['level'] = 0;
            }
            $result = Db::name('data_type')->insert($param);
            if( $result ) {
                xn_add_admin_log('添加数据类型');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $list = Db::name('data_type')->select()->toArray();
        $list = Data::tree($list, 'title','id');
        return view('form',['list'=>$list,'pid'=>$this->request->get('pid')])->filter(function($content){
            return str_replace("&amp;emsp;",'&emsp;',$content);
        });
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if($param['pid']){
                $data = Db::name('data_type')->field('level')->find($param['pid']);
                $param['level'] = ($data['level']+1);
            }else{
                $param['level'] = 0;
            }
            $result = Db::name('data_type')->update($param);
            if( $result ) {
                xn_add_admin_log('修改行业类型');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = $this->request->get('id');
        $data = Db::name('data_type')->where('id',$id)->find();
        $list = Db::name('data_type')->select();
        $list = Data::tree($list, 'title','id');
        return view('form',['data'=>$data,'list'=>$list,'pid'=>$data['pid']])->filter(function($content){
            return str_replace("&amp;emsp;",'&emsp;',$content);
        });
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name('data_type')->delete($id);
        xn_add_admin_log('删除行业类型');
        $this->success('删除成功');
    }

    public function sort()
    {
        $param = $this->request->post();
        foreach ($param as $k => $v) {
            Db::name('data_type')->where('id', $k)->save(['sort'=>$v]);
        }
        $this->success('排序成功');
    }

    public function getSonType()
    {
        $param = $this->request->param();
        if( empty($param['pid']) ){
            return json([
                'status' => 0,
                'msg' => '参数错误'
            ]);
        }
        $data = [];
        $data = Db::name('data_type')->field('id,title')->where('pid',$param['pid'])->select()->toArray();
        return json([
            'status' => 1,
            'msg' => '请求成功',
            'data' => $data
        ]);
    }
}
